package cn.iocoder.yudao.framework.weixin;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import me.chanjar.weixin.cp.tp.message.WxCpTpMessageRouter;
import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceImpl;

/**
 * @Author: lvliangjun
 * @Date: 2024/5/22 18:34
 */
public class T1 {


    //服务商的
    static String providercorpid = "ww00b36ed5acf38dc9";
    static String providerSecret = "gFjBpLXZcwSYCdI6hq2bMxBnlw-v1Xkk0WbTJJVhAAZ1soHfXgF0Owpi-yKl9AgD";

    //模板的
    static String Token = "TPJ4Fdrpasg4R";
    static String encodingAESKey = "msJgW0YBhnDkKQnGz8gtdSFnSarTSIxztBXWxicDPBR";
    //模板ID：dk83899bb2c060ce44
    //模板Secret：iK3sD1n5ovlRTDOl424mu5uGKO_VgXS5oa7V2Yf9UMA
    static String corpid = "dk83899bb2c060ce44";
    static String secret = "iK3sD1n5ovlRTDOl424mu5uGKO_VgXS5oa7V2Yf9UMA";
    public static void main(String[] args) throws WxErrorException {
        String xml = "<xml><ToUserName><![CDATA[dk83899bb2c060ce44]]></ToUserName><Encrypt><![CDATA[FbHXOWyYICdrKs20Y2xZwu/srL2dw6sx761jihInUP9h7dKeriwDnBn7HPjt259MWXxJNmkh47aUptmcHpkTzpcWDkVUw7kpbQDhET57qAe5EAvyJ8zLUBcWEpr0aDmCnjhSyxNnp7DhG6jIjkcjwMQiAzXJOZFtEaaSx97BUTTWmh3Nlyb8kLuhyM/IXMjHHa9JGBWKhFDlpQpshHFnhnCQ6dQznfHN897ISBjsLW2KrL6s8MHF0+2mCifQ/fq9bplzPsDbDVeDXzFS2MJ1QpxJzlfUbo9nGKDRip5i3zmbXLrdb5IP/bECIujg9A9u1khJYD9wxmJMGIeuZs1B/NuONXlaDB2lbW58p5iOwdpJyRIplZKvs41hCIAu/3Z0]]></Encrypt><AgentID><![CDATA[]]></AgentID></xml>";

         String timestamp="1716377399";
         String nonce="1716205092";
         String msgSignature="9179cdb2b409db39e64454b9ac35c8047301186a";

        WxCpTpServiceImpl baseWxCpService = new WxCpTpServiceImpl();
        WxCpTpDefaultConfigImpl wxCpTpConfigStorage = new WxCpTpDefaultConfigImpl();
        wxCpTpConfigStorage.setCorpId(corpid);
        wxCpTpConfigStorage.setCorpSecret(secret);
        wxCpTpConfigStorage.setToken(Token);
        wxCpTpConfigStorage.setAesKey(encodingAESKey);

        baseWxCpService.setWxCpTpConfigStorage(wxCpTpConfigStorage);



        WxCpTpXmlMessage inMessage = WxCpTpXmlMessage.fromEncryptedXml(xml, baseWxCpService.getWxCpTpConfigStorage(), timestamp, nonce, msgSignature);
        System.out.println(inMessage.getToUserName());
        System.out.println(inMessage.getSuiteTicket());



        WxCpTpMessageRouter wxCpMessageRouter = new WxCpTpMessageRouter(baseWxCpService);
        WxCpXmlOutMessage outMessage = wxCpMessageRouter.route(inMessage);
        if (outMessage != null) {
            System.out.println(outMessage.toEncryptedXml(wxCpTpConfigStorage));
        }
        getSuiteAccessToken(inMessage.getSuiteTicket());

    }
    public static void getSuiteAccessToken(String suiteTicket) throws WxErrorException {
        WxCpTpServiceImpl baseWxCpService = new WxCpTpServiceImpl();
        WxCpTpDefaultConfigImpl wxCpTpConfigStorage = new WxCpTpDefaultConfigImpl();
        wxCpTpConfigStorage.setSuiteId(corpid);
        wxCpTpConfigStorage.setSuiteSecret(secret);


//        wxCpTpConfigStorage.setSuiteTicket("Iw8BmGATuXLo5G4eHmMmpxMFrIuwKRA4No7j2THNT0TQc95dBjsxvQ7TJsnUfVFn");
//        wxCpTpConfigStorage.setSuiteTicketExpiresTime();
        baseWxCpService.setWxCpTpConfigStorage(wxCpTpConfigStorage);
        baseWxCpService.setSuiteTicket(suiteTicket);
        String suiteAccessToken = baseWxCpService.getSuiteAccessToken();
        System.out.println(suiteAccessToken);

    }
}
